package com.zs4sdk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zs4sdk.Zs4GameBase;
import com.zs4sdk.Zs4GameMenu;
import com.zs4sdk.common.Extra;
import com.common.library.data.CommonEventHandler;
import com.common.library.data.CommonHandle;
import com.common.library.data.EventHandler2Get;
import com.common.library.entity.JSONResultEntity;
import com.common.library.util.ActivityHelper;
import com.common.library.util.EncryptHelper;
import com.common.library.util.HandlerUtil;
import com.common.library.util.Helper;
import com.common.library.util.ParamsHelper;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * function: 重置密码
 * <p/>
 * Created by Jancy_Weng on 2015/8/20.
 */
public class Zs4GameChangePasswordActivity extends Zs4GameBaseActivity implements View.OnClickListener {

    public static final String TAG = Zs4GameChangePasswordActivity.class.getSimpleName();
    //新密码输入框
    private EditText mOldPasswordView;
    //新密码输入框
    private EditText mNewPasswordView;
    //新密码再次输入框
    private EditText enSureNewPasswordView;
    //忘记密码？
    private TextView forgetPwdTextView;

    //提交新密码
    private Button mSubmitView;
    //提示
    private TextView sunmitTipTextView;
    //手机号
    private String mobile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ActivityHelper.getLayoutResId("zs4gamelayout_act_change_password"));

        getParams();

        init();

    }


    @Override
    protected void init() {
        super.init();

        setTopTitle(ActivityHelper.getStringResId("zs4gamestring_text_change_login_password"));

        mOldPasswordView = findView(EditText.class, ActivityHelper.getIdResId("zs4gameviewid_mNewPasswordView_act_old_password"));
        mNewPasswordView = findView(EditText.class, ActivityHelper.getIdResId("zs4gameviewid_mNewPasswordView_act_reset_password"));
        enSureNewPasswordView  = findView(EditText.class, ActivityHelper.getIdResId("zs4gameviewid_mNewPasswordView_act_sure_reset_password"));

        mSubmitView = findView(Button.class, ActivityHelper.getIdResId("zs4gameviewid_mSubmitView_act_reset_password"));
        sunmitTipTextView =  findView(TextView.class, ActivityHelper.getIdResId("zs4gameviewid_textview_retrivepassword_phone"));
        forgetPwdTextView =  findView(TextView.class, ActivityHelper.getIdResId("zs4gameviewid_textview_forget_pwd"));

        mSubmitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cilcksubmit();
            }
        });

        forgetPwdTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goRetrievePasswordActivity();
            }
        });


    }


    //获得参数
    public void getParams() {
        Intent intent = this.getIntent();
        mobile = intent.getStringExtra("mobile");
    }


    //点击提交后的处理
    public void cilcksubmit() {
        String oldPassword = mOldPasswordView.getText().toString().trim();
        String newPassword = mNewPasswordView.getText().toString().trim();
        String enSureNewPassword = enSureNewPasswordView.getText().toString().trim();
        if(oldPassword.equals("")||newPassword.equals("")||enSureNewPassword.equals("")){
         return;
        }

        if (oldPassword.length() < 6 || oldPassword.length() > 12) {
            //没有密码长度
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_length"));
            return;
        }
        if (newPassword.length() < 6 || newPassword.length() > 12) {
            //没有密码长度
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_length"));
            return;
        } else if (Helper.checkChinese(newPassword)) {
            // 密码为非中文
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_unchinese"));
            return;
        }else if(newPassword.equals(oldPassword)) {
            //两次密码不一样
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_new_equals_old"));
            return;
        } else if(!newPassword.equals(enSureNewPassword)) {
            //两次密码不一样
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_new_no_equals_affirm"));
            return;
        }
        else {
            httpGetForgetCodeNewPassword(oldPassword, newPassword);
        }

    }

    // 到找回密码
    private void goRetrievePasswordActivity() {

        Bundle bundle = new Bundle();
        bundle.putInt(Extra.INTENTKEY.SCREEN_ORIENTATION, this.mScreenOrientation);
        // 根据类型，来控制页面显示
        bundle.putInt(Extra.INTENTKEY.TITLE, Extra.REQUESTCODE.RESETPASSWORD);
        bundle.putString(Extra.INTENTKEY.FLAG, this.flag);
        Intent intent = new Intent(this, Zs4GameForgetPasswordActivity.class);
        intent.putExtras(bundle);
        startActivityForResult(intent, Extra.REQUESTCODE.REQUESTRetrievePassword);
    }

    /**
     * 请求重置密码
     *
     * @param oldPassword
     * @param newPassword
     */
    private void httpGetForgetCodeNewPassword(final String oldPassword, final String newPassword) {

        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.SHOWPROCESS);

        ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
//        paramList.add(new BasicNameValuePair("v", Extra.VERSION_API));
        paramList.add(new BasicNameValuePair("oldpassword",EncryptHelper.encryptByAES(EncryptHelper.md5(oldPassword))));
        paramList.add(new BasicNameValuePair("newpassword", EncryptHelper.encryptByAES(EncryptHelper.md5(newPassword))));

        new EventHandler2Get(
                Extra.URL.CHANGE_PASSWORD,
                paramList,
                ParamsHelper.getUserAgent1(),
                new CommonEventHandler.ResultHandlerInEvent() {

                    @Override
                    public void doResultHanler(JSONObject result) throws JSONException {

                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);

                        Message msg = myHandle.obtainMessage();

                        msg.what = Extra.MessageKey.REQUEST_SUCCESS;
                        msg.obj = result;

                        // 发送消息
                        myHandle.sendMessage(msg);

                    }

                    @Override
                    public void doExceptionHandler() {
                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);
                    }
                });
    }


    CommonHandle myHandle = new CommonHandle(Zs4GameChangePasswordActivity.this, new CommonHandle.HandleMessage() {

        @Override
        public void doHandleMessage(Message msg) {

            if (Extra.MessageKey.REQUEST_SUCCESS == msg.what) {
                JSONResultEntity entity = new JSONResultEntity((JSONObject) msg.obj);
                if (entity.getResult() == Extra.RESULTCODE.SUCCESS) {
//                    ActivityHelper.showToast(ActivityHelper.getStringResId("zs4gamestring_hint_please_login_afterresetpassword"));
                    // sdk账号注销
                    String action = getPackageName().concat(Extra.ACTIONKEY.LOGOUT_POSTFIX);
                    Zs4GameMenu.logoutHandle(Zs4GameChangePasswordActivity.this, action,
                            Zs4GameBase.getPackageName(),ActivityHelper.getString(ActivityHelper.getStringResId("zs4gamestring_hint_password_change_success")));
                    finish();
                } else {
                    //请求失败提示
                    sunmitTipTextView.setText(entity.getDesc());
                }

            }

        }
    });

    @Override
    protected int getLeftBtnType() {
        return TYPE_LEFTBTN_BACK;
    }

    @Override
    protected int getRightBtnType() {
        return TYPE_RIGHTBTN_INVISIBLE;
    }


}
